<?php

namespace app\admin\controller;


// 文章管理控制器
use think\Route;

class Article extends Base
{

    // 文章列表
    public function artlist()
    {
        $articles = model("Article")->with('category')
            ->order(['is_top' => 'desc', 'create_time' => 'desc'])
            ->paginate(10);

        $this->assign('articles', $articles);
        return view();
    }

    // 文章添加
    public function artadd()
    {
        // post提交
        if (request()->isAjax()) {
            $data = [
                'title' => input('post.title'),
                'cate_id' => input('post.cateid'),
                'flag' => input('post.flag'),
                'tags' => input('post.tags'),
                'is_top' => input('post.is_top', 0),
                'desc' => input('post.desc'),
                'content' => input('post.content'),
            ];
            $result = model('Article')->add($data);
            if ($result == 1) {
                $this->success('文章添加成功', 'admin/article/artlist');
            } else {
                $this->error($result);
            }

        }


        // get请求
        $cates = model('Category')->select();
        $this->assign('cates', $cates);
        return view();
    }


    // 文章置顶(推荐)
    public function top()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('post.id'),
                'is_top' => input('post.is_top') ? 0 : 1,
            ];
            $result = model('Article')->top($data);
            if ($result == 1) {
                $this->success('操作成功', 'admin/article/artlist');
            } else {
                $this->error($result);
            }
        }
    }

    // 文章编辑
    public function artedit()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('post.id'),
                'title' => input('post.title'),
                'cate_id' => input('post.cateid'),
                'flag' => input('post.flag'),
                'tags' => input('post.tags'),
                'is_top' => input('post.is_top', 0),
                'desc' => input('post.desc'),
                'content' => input('post.content'),
            ];

            $result = model('Article')->edit($data);
            if ($result == 1) {
                $this->success('文章添加成功', 'admin/article/artlist');
            } else {
                $this->error($result);
            }
        }


        // get请求
        $article_info = model('Article')->find(input('id'));
        if (!$article_info) {
            abort(404, '文章未找到');
        }
        $this->assign('articleInfo', $article_info);

        $cates = model('Category')->select();
        $this->assign('cates', $cates);
        return view();
    }

    // 文章删除
    public function artdel()
    {
        if (request()->isAjax()){
            $articl_info = model('Article')
                ->with('comments')
                ->find(input('post.id'));
            if (!$articl_info){
                abort('404', '该文章未找到');
            }
            // 联级删除文章下的所有评论
            $result = $articl_info->together('comments')->delete();
            if ($result){
                $this->success('文章删除成功', 'admin/article/artlist');
            }else{
                $this->error('文章删除失败');
            }
        }
    }

}
